//package top.xierongsheng.user.controller;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//import top.xierongsheng.code.CacheVariable;
//import top.xierongsheng.code.R;
//import top.xierongsheng.code.aspect.log.Log;
//import top.xierongsheng.code.aspect.log.LogType;
//import top.xierongsheng.code.oss.OSSClientService;
//import top.xierongsheng.user.dto.LoginDto;
//import top.xierongsheng.user.model.SYS_ROLE;
//import top.xierongsheng.user.model.SYS_USER;
//import top.xierongsheng.user.service.IRoleResourceService;
//import top.xierongsheng.user.service.IRoleService;
//import top.xierongsheng.user.service.IUserService;
//import top.xierongsheng.code.redis.RedisService;
//
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.List;
//
//@Slf4j
//@RestController //这意味着这个类是一个控制器
//@RequestMapping(path = "/system") //这意味着URL以/system（在应用程序路径之后）
//@CrossOrigin
//@RequiredArgsConstructor
//@Tag(name = "系统用户认证相关接口")
//public class UserSysController {
//
//    private final IUserService userService;
//
//    private final IRoleService roleService;
//
//    private final IRoleResourceService roleResourceService;
//
//    private final RedisService redisService;
//
//
//    private static final String[] fileImgType = {"image/png", "image/jpeg", "image/gif", "application/x-jpg", "application/x-png"};
//
//    private static final String[] fileAudioType = {"audio/mp3"};
//
//    private static final String[] fileVideoType = {"video/mp4"};
//
//
//    /**
//     * 登录
//     *
//     * @param userId
//     * @param loginDto
//     * @return
//     */
//    @PostMapping(path = "/login", consumes = {"application/json", "application/x-www-form-urlencoded"})  //仅映射POST请求
//    @Log(type = LogType.web, desc = "管理后台登录日志")
//    @Operation(summary = "登录")
//    public R login(@RequestHeader(name = "userId", required = false) String userId, @RequestBody @Validated LoginDto loginDto) {
//        //根据账号密码获取用户信息
//        SYS_USER sysUser = userService.getOne(new QueryWrapper<SYS_USER>().lambda().eq(SYS_USER::getAccount, loginDto.getAccount()).eq(SYS_USER::getPassword, loginDto.getPassword()), false);
//        if (sysUser == null) {
//            return R.fail("用户名或密码错误");
//        }
//        if (sysUser.getStatus() == 0 || sysUser.getStatus() == 2) {
//            return R.fail("该用户当前不可用，请联系管理员");
//        }
//        //获取用户角色
//        SYS_ROLE sysRole = roleService.getById(sysUser.getRoleId());
//        if (sysRole.getStatus() == 2) {
//            return R.fail("该用户所属角色不可用，请联系管理员");
//        }
//        //获取用户权限菜单
//        List<HashMap> menuList = roleResourceService.getMenuByRoleId(sysUser.getRoleId());
//        if (menuList.size() == 0) {
//            return R.fail("该用户没有配置权限菜单，请联系管理员");
//        }
//        HashMap baseReturn = new HashMap();
//        baseReturn.put("userInfo", sysUser);
//        baseReturn.put("roleInfo", sysRole);
//        redisService.set(CacheVariable.USER + sysUser.getId(), sysUser, CacheVariable.USER_EXPIRE);
//        redisService.set(CacheVariable.USER_ROLE + sysRole.getId(), sysRole, CacheVariable.USER_ROLE_EXPIRE);
//        redisService.set(CacheVariable.ROLE_RESOURCE + sysRole.getId(), menuList, CacheVariable.ROLE_RESOURCE_EXPIRE);
//        return R.ok("登录成功").zip("data", baseReturn);
//    }
//
//    /**
//     * 登出
//     *
//     * @param userId
//     * @return
//     */
//    @PostMapping(path = "/signOut", consumes = {"application/json", "application/x-www-form-urlencoded"})
//    @Log(type = LogType.web, desc = "管理后台登出日志")
//    @Operation(summary = "登出")
//    public R signOut(@RequestHeader(name = "userId") String userId) {
//        redisService.remove(CacheVariable.USER + userId);
//        return R.ok();
//    }
//
//    /**
//     * 获取用户权限菜单
//     *
//     * @param userId
//     * @return
//     */
//    @GetMapping(path = "/getUserPermissionMenu")
//    @Operation(summary = "获取用户权限菜单")
//    public R getUserPermissionMenu(@RequestHeader(name = "userId") String userId) {
//        Object userInfo = redisService.get(CacheVariable.USER + userId);
//        SYS_USER user = (SYS_USER) userInfo;
//        Object resourceInfo = redisService.get(CacheVariable.ROLE_RESOURCE + user.getRoleId());
//        List<HashMap> menuList = (List<HashMap>) resourceInfo;
//        if (menuList.size() == 0) {
//            return R.powererror("该用户没有配置权限菜单，请联系管理员");
//        }
//        return R.ok().zip("data", menuList);
//    }
//
//
//    /**
//     * 文件上传
//     *
//     * @param userId
//     * @param file
//     * @return
//     * @auther 谢荣生
//     * @datetime 2021/1/7 10:05
//     */
//    @PostMapping(path = "/uploadFile")
//    @Operation(summary = "文件上传")
//    public R uploadFile(@RequestHeader(name = "userId") String userId, MultipartFile file) throws IOException {
//        if (!file.isEmpty()) {
//            //文件类型图片
//            if (Arrays.asList(fileImgType).contains(file.getContentType())) {
//                return R.ok("图片上传成功").zip("data", OSSClientService.uploadFileTemp(file));
//            }
//            //文件类型音频
//            if (Arrays.asList(fileAudioType).contains(file.getContentType())) {
//                return R.ok("音频上传成功").zip("data", OSSClientService.uploadFileTemp(file));
//            }
//            //文件类型视频
//            if (Arrays.asList(fileVideoType).contains(file.getContentType())) {
//                return R.ok("视频上传成功").zip("data", OSSClientService.uploadFileTemp(file));
//            }
//            return R.fail(file.getContentType() + "文件类型不支持，请重新选择上传");
//        } else {
//            return R.fail("请选择文件上传");
//        }
//    }
//}
